/* 用户模块的vuex操作 */
import {
  setStorageToken,
  getStorageToken,
  removeStorageToken
} from '@/utils/stroage'

import { getUserInfo } from '@/api/user'

export default {
  namespaced: true,
  state: {
    // 保存登陆成功的token信息
    tokenobj: getStorageToken(),
    userInfo: {}
  },
  getters: {},
  mutations: {
    // 设置操作数据
    setToken(state, payload) {
      state.tokenobj = payload
      // 刷新后vuex里面的tokenObj就会清空，需要本地存储
      // localStorage.setItem('hm-news-token', JSON.stringify(payload))
      setStorageToken(payload)
    },
    removeToken(state) {
      state.tokenobj = {}
      removeStorageToken()
    },
    setUserInfo(state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    async getUserInfoAsnyc(context) {
      const res = await getUserInfo()
      context.commit('setUserInfo', res.data)
    }
  },
  modules: {}
}
